/**
 * @author 徐楠
 * @date 2021/12/22 11:05
 * @version 1.0
 */

package com.xunan.likou;

import java.util.*;

import java.util.Map.Entry;

public class RelativeRanking {
    public static void main(String[] args) {
        int[] score = {8, 4, 6, 2, 5, 7};
        String[] result = findRelativeRanks(score);
        for (String s : result) {
            System.out.println(s);
        }
    }

    public static String[] findRelativeRanks(int[] score) {

        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < score.length; i++) {
            map.put(i, score[i]);
        }

        //将map.entrySet()转换成list
        List<Map.Entry<Integer, Integer>> list = new ArrayList<Map.Entry<Integer, Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
            //降序排序
            @Override
            public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) {
                //return o1.getValue().compareTo(o2.getValue());
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        for (Map.Entry<Integer, Integer> mapping : list) {
            System.out.println(mapping.getKey() + ":" + mapping.getValue());
        }
        System.out.println("-----------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getValue());
        }
        System.out.println("-----------");

        String[] result = new String[list.size()];
        int ranking = 1;
        int count = 0;
        System.out.println("=====");
        for (Map.Entry<Integer, Integer> mapping : list) {
            count++;
            result[mapping.getKey()] = ranking++ + "";
            if (count == 1) {
                result[mapping.getKey()] = "Gold Medal";
            }
            if (count == 2) {
                result[mapping.getKey()] = "Silver Medal";
            }
            if (count == 3) {
                result[mapping.getKey()] = "Bronze Medal";
            }

            System.out.println(mapping.getKey() + "....." + result[mapping.getKey()]);
        }


        for (String s : result) {
            System.out.println(s);
        }


        return result;
    }

    public static String[] findRelativeRanks1(int[] score) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < score.length; ++i) {
            map.put(score[i], i);
        }
        Arrays.sort(score);
        String[] result = new String[score.length];
        for (int i = 0; i < score.length; ++i) {
            int s = map.get(score[i]);
            if (i == score.length - 1) result[s] = "Gold Medal";
            else if (i == score.length - 2) result[s] = "Silver Medal";
            else if (i == score.length - 3) result[s] = "Bronze Medal";
            else result[s] = String.valueOf(score.length - i);
        }
        return result;
    }


}
